package com.danco.training.bookshop.service.file;

import java.io.IOException;

import org.apache.log4j.Logger;

import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.danco.training.TextFileWorker;
import com.danco.training.bookshop.entity.Book;
import com.danco.training.bookshop.entity.Order;
import com.danco.training.bookshop.entity.storage.Bookshop;
import com.danco.training.bookshop.entity.storage.OrderStorage;
import com.danco.training.bookshop.service.storage.OrderStorageService;
//import com.danco.training.controller.OrderController;
//import com.danco.training.view.Separator;


public class OrderStorageFile {
	
	private OrderStorage orderStorage = OrderStorage.getOrderStorageInstance();
	private OrderStorageService orderStorageService = Bookshop.getBookshopInstance().getOrderStorageService();
	private TextFileWorker textFileWorker;
	//private OrderController orderController = new OrderController();
	private static final Logger logger = Logger.getLogger(OrderStorageFile.class);

	public OrderStorageFile(OrderStorage orderStorage, String FILE)
			throws IOException {
try{
		Path filePath = Paths.get(FILE);
		Files.createFile(filePath);
		this.orderStorage = orderStorage;
		textFileWorker = new TextFileWorker(FILE);
}

catch (FileAlreadyExistsException e){
	logger.error(e.getMessage(), e);
	Files.deleteIfExists( Paths.get(FILE));			
	Path filePath = Paths.get(FILE);
	Files.createFile(filePath);
	this.orderStorage = orderStorage;
	textFileWorker = new TextFileWorker(FILE);
	
}
catch (Exception e){
	logger.error(e.getMessage(), e);
}
}
	

	public void saveOrderToFile(){
		try{
		String[] orders = new String[orderStorage.getOrders().size()];
		for (int i = 0; i < orders.length; i++) {
	//		orders[i] = orderController.orderDetails(orderStorage.getOrders().get(i).getId());
		}
		textFileWorker.writeToFile(orders);
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
		}
	}
	
	public List<Order> readOrdersFromFile() {

		List<Order> orders = new ArrayList<Order>();
//		try{
//		List<Book> books = new ArrayList<Book>();
//		String[] ordersArray = textFileWorker.readFromFile();
//		
//		
//		for (int i = 0; i < ordersArray.length; i++) {
//			int begin = ordersArray[i].indexOf(':');
//			int executed = ordersArray[i].indexOf(':', begin) ;
		//	int endBooks = ordersArray[i].indexOf(Separator.ORDER_SEPARATOR.toString() ) ;
		//	int endExecuted = ordersArray[i].indexOf(Separator.ORDER_SEPARATOR.toString(), executed) ;
		//	int tp = ordersArray[i].lastIndexOf(':') ;
		//	for ( int j = begin+1; j != endBooks; j++){
		//	int index1 = ordersArray[i].indexOf(Separator.BOOK_SEPARATOR_IN_ORDER.toString(), j);			
				
		//		Book b = new Book(ordersArray[i].substring(j, index1));
		//		books.add(b);
//				
//				j= index1;
//				
//			}
//			Order order = new Order(Integer.valueOf(ordersArray[i].substring(0, begin)));
//			order.setBooks(books);
//			order.setExecuted(Boolean.getBoolean(ordersArray[i].substring(executed, endExecuted)));
//			order.setTotalPrice(Double.valueOf(ordersArray[i].substring(tp+1)));
//			orders.add(order);
//			}
//			
//		}
//		catch (StringIndexOutOfBoundsException e){
//			logger.error(e.getMessage(), e);
//		}
//        catch (Exception e){
//        	logger.error(e.getMessage(), e);
//        	
//	}
			
		return orders;

	}

}
